using UnityEngine;
using UnityEngine.UI;
namespace CGF
{
    public partial class PanelPersonalInfo : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        public static void Open()
        {
            UIManagerComponent.instance.OpenUI<PanelPersonalInfo>(UILayer.Normal, RootComponent.instance, false);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelPersonalInfo>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelPersonalInfo", layer, isFloat);
            BindProperties(transform);

            BtnBack.SetClick(Close);

            CellNickname.SetClick(() =>
            {
                string columnName = CellNickname.gameObject.Get<Text>("Title").text;
                string defaultValue = CellNickname.gameObject.Get<Text>("Content").text;
                PanelEditUserInfo.Open(columnName, "user_name", defaultValue);
            });

            CellPhone.enabled = string.IsNullOrEmpty(UserInfo.Info["phone"].ToString());
            CellPhone.gameObject.Get<GameObject>("JianTou").SetActive(CellPhone.enabled);
            CellPhone.SetClick(() =>
            {
                PanelEditPhone.Open();
            });

            CellEmail.enabled = string.IsNullOrEmpty(UserInfo.Info["email"].ToString());
            CellEmail.gameObject.Get<GameObject>("JianTou").SetActive(CellEmail.enabled);
            CellEmail.SetClick(() =>
            {
                PanelEditEmail.Open();
            });

            CellWhatsApp.SetClick(() =>
            {
                string columnName = CellWhatsApp.gameObject.Get<Text>("Title").text;
                string defaultValue = CellWhatsApp.gameObject.Get<Text>("Content").text;
                PanelEditUserInfo.Open(columnName, "what_app", defaultValue);
            });
            
            CellLoginPasswd.SetClick(() =>
            {
                PanelEditPassword.Open(PanelEditPassword.Type.LoginPasswd);
            });

            CellWithdrawPasswd.SetClick(() =>
            {
                PanelEditPassword.Open(PanelEditPassword.Type.WithdrawPasswd);
            });

            bool isUsernameLogin = UserInfo.IsUsernameLogin;
            LoginPasswdLine.gameObject.SetActive(isUsernameLogin);
            CellLoginPasswd.gameObject.SetActive(isUsernameLogin);
            List.rectTransform.sizeDelta = new Vector2(List.rectTransform.sizeDelta.x, isUsernameLogin ? 615 : 510);

            InitData();
            EventManager.Add<Event_UserInfoUpdate>(OnUserInfoUpdate);
        }
        private void OnUserInfoUpdate(Event_UserInfoUpdate e)
        {
            InitData();
        }

        private void InitData()
        {
            CellNickname.gameObject.Get<Text>("Content").text = UserInfo.Info["name"].ToString();
            CellPhone.gameObject.Get<Text>("Content").text = UserInfo.Info["phone"].ToString();
            CellEmail.gameObject.Get<Text>("Content").text = UserInfo.Info["email"].ToString();
            CellWhatsApp.gameObject.Get<Text>("Content").text = UserInfo.Info["what_app"].ToString();
            CellWithdrawPasswd.gameObject.Get<Text>("Content").text = string.IsNullOrEmpty(UserInfo.Info["withdrawal_password"].ToString()) ? "" : "******";
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
            EventManager.Remove<Event_UserInfoUpdate>(OnUserInfoUpdate);
        }

    }
}
